{% extends "base.html" %}

{% block title %}配置管理 - Alist-MikananiRss WebUI{% endblock %}

{% block content %}
<div class="fade-in">
    <!-- 页面标题 -->
    <div class="page-header border-bottom pt-3 pb-2 mb-3">
        <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-2">
            <div>
                <h1 class="h2 mb-0">
                    <i class="bi bi-gear me-2"></i>
                    配置管理
                </h1>
                <p class="text-muted small mb-0 d-md-none">在移动设备上快速查看与编辑核心配置</p>
            </div>
            <div class="d-flex gap-2 flex-md-row flex-column w-100 w-md-auto" id="configActionBar">
                <div class="btn-toolbar d-none d-md-block">
                    <div class="btn-group">
                        <button type="button" class="btn btn-sm btn-outline-secondary config-action" data-action="reload">
                            <i class="bi bi-arrow-clockwise me-1"></i>
                            重新加载
                        </button>
                        <button type="button" class="btn btn-sm btn-outline-secondary config-action" data-action="export">
                            <i class="bi bi-download me-1"></i>
                            导出
                        </button>
                        <button type="button" class="btn btn-sm btn-outline-secondary config-action" data-action="import">
                            <i class="bi bi-upload me-1"></i>
                            导入
                        </button>
                    </div>
                </div>
                <div class="dropdown d-md-none">
                    <button class="btn btn-outline-secondary btn-sm w-100 dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        配置操作
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end w-100">
                        <li><button class="dropdown-item config-action" type="button" data-action="reload"><i class="bi bi-arrow-clockwise me-2"></i>重新加载</button></li>
                        <li><button class="dropdown-item config-action" type="button" data-action="export"><i class="bi bi-download me-2"></i>导出</button></li>
                        <li><button class="dropdown-item config-action" type="button" data-action="import"><i class="bi bi-upload me-2"></i>导入</button></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <div class="alert alert-warning d-none" id="configSetupNotice" role="alert">
        <i class="bi bi-exclamation-circle me-2"></i>
        检测到缺失或损坏的配置，请完成必填项后保存以激活系统。
    </div>

    <div class="card border-0 shadow-sm d-none" id="configTestPanel">
        <div class="card-body">
            <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
                <div class="d-flex align-items-center gap-2">
                    <i class="bi bi-activity text-primary"></i>
                    <div>
                        <div class="fw-semibold">连通性与验证</div>
                        <div class="text-muted small" id="configTestHint">运行“测试配置”后展示 Alist / RSS 等外部依赖的检查结果</div>
                    </div>
                </div>
                <span class="badge rounded-pill bg-secondary" id="configTestStatusBadge">未检测</span>
            </div>
            <div class="row row-cols-1 row-cols-md-3 g-3 mt-3" id="connectivityResults"></div>
        </div>
    </div>

    <!-- 配置选项卡 -->
    <div class="row">
        <div class="col-12">
            <ul class="nav nav-tabs flex-nowrap" id="configTabs" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="common-tab" data-bs-toggle="tab" data-bs-target="#common" type="button" role="tab">
                        <i class="bi bi-gear me-2"></i>
                        基础配置
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="alist-tab" data-bs-toggle="tab" data-bs-target="#alist" type="button" role="tab">
                        <i class="bi bi-server me-2"></i>
                        Alist配置
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="mikan-tab" data-bs-toggle="tab" data-bs-target="#mikan" type="button" role="tab">
                        <i class="bi bi-rss me-2"></i>
                        蜜柑配置
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="rename-tab" data-bs-toggle="tab" data-bs-target="#rename" type="button" role="tab">
                        <i class="bi bi-magic me-2"></i>
                        重命名配置
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="notification-tab" data-bs-toggle="tab" data-bs-target="#notification" type="button" role="tab">
                        <i class="bi bi-bell me-2"></i>
                        通知配置
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="webdav-tab" data-bs-toggle="tab" data-bs-target="#webdav" type="button" role="tab">
                        <i class="bi bi-folder me-2"></i>
                        WebDAV配置
                    </button>
                </li>
            </ul>

            <div class="tab-content" id="configTabContent">
                <!-- 基础配置 -->
                <div class="tab-pane fade show active" id="common" role="tabpanel">
                    <div class="card mt-3">
                        <div class="card-body">
                            <form id="commonForm" class="row g-3">
                                <div class="col-md-6">
                                    <label for="interval_time" class="form-label">RSS检查间隔 (秒)</label>
                                    <input type="number" class="form-control" id="interval_time" min="60" step="1">
                                    <div class="form-text">RSS订阅源的检查间隔时间，最小60秒</div>
                                </div>
                                <div class="col-md-6">
                                    <label for="log_level" class="form-label">日志级别</label>
                                    <select class="form-select" id="log_level">
                                        <option value="DEBUG">DEBUG</option>
                                        <option value="INFO">INFO</option>
                                        <option value="WARNING">WARNING</option>
                                        <option value="ERROR">ERROR</option>
                                        <option value="CRITICAL">CRITICAL</option>
                                    </select>
                                    <div class="form-text">系统日志记录级别</div>
                                </div>
                                <div class="col-md-6">
                                    <label for="proxy_http" class="form-label">HTTP代理</label>
                                    <input type="url" class="form-control" id="proxy_http" placeholder="http://proxy.example.com:8080">
                                    <div class="form-text">HTTP请求代理地址（可选）</div>
                                </div>
                                <div class="col-md-6">
                                    <label for="proxy_https" class="form-label">HTTPS代理</label>
                                    <input type="url" class="form-control" id="proxy_https" placeholder="https://proxy.example.com:8080">
                                    <div class="form-text">HTTPS请求代理地址（可选）</div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- Alist配置 -->
                <div class="tab-pane fade" id="alist" role="tabpanel">
                    <div class="card mt-3">
                        <div class="card-body">
                            <form id="alistForm" class="row g-3">
                                <div class="col-md-6">
                                    <label for="alist_base_url" class="form-label">Alist服务器地址</label>
                                    <input type="url" class="form-control" id="alist_base_url" required>
                                    <div class="form-text">Alist服务器的完整URL地址</div>
                                </div>
                                <div class="col-md-6">
                                    <label for="alist_token" class="form-label">访问令牌</label>
                                    <input type="password" class="form-control" id="alist_token" required>
                                    <div class="form-text">Alist API访问令牌</div>
                                </div>
                                <div class="col-md-4">
                                    <label for="alist_downloader" class="form-label">下载器</label>
                                    <select class="form-select" id="alist_downloader">
                                        <option value="qBittorrent">qBittorrent</option>
                                        <option value="aria2">Aria2</option>
                                        <option value="115 Cloud">115 Cloud</option>
                                        <option value="115 Open">115 Open</option>
                                    </select>
                                    <div class="form-text">选择使用的下载器</div>
                                </div>
                                <div class="col-md-8">
                                    <label for="alist_download_path" class="form-label">下载路径</label>
                                    <input type="text" class="form-control" id="alist_download_path" required>
                                    <div class="form-text">Alist中的下载目标路径</div>
                                </div>
                                <div class="col-12">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="alist_convert_torrent">
                                        <label class="form-check-label" for="alist_convert_torrent">
                                            将种子文件转换为磁力链接
                                        </label>
                                        <div class="form-text">自动将.torrent文件转换为magnet链接进行下载</div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- 蜜柑配置 -->
                <div class="tab-pane fade" id="mikan" role="tabpanel">
                    <div class="card mt-3">
                        <div class="card-body">
                            <form id="mikanForm">
                                <div class="mb-3">
                                    <label class="form-label">RSS订阅源</label>
                                    <div id="subscribeUrls" class="d-flex flex-column gap-2"></div>
                                    <button type="button" class="btn btn-sm btn-outline-success mt-2" data-action="add-subscribe">
                                        <i class="bi bi-plus"></i> 新增订阅源
                                    </button>
                                    <div class="form-text">蜜柑计划的RSS订阅地址，支持多个订阅源</div>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">正则规则定义</label>
                                    <div id="regexPatterns" class="d-flex flex-column gap-2"></div>
                                    <button type="button" class="btn btn-sm btn-outline-success mt-2" data-action="add-regex">
                                        <i class="bi bi-plus"></i> 新增规则
                                    </button>
                                    <div class="form-text">定义可复用的规则名称与对应正则表达式，filters 将引用这些名称</div>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">启用过滤规则</label>
                                    <div id="filters" class="d-flex flex-column gap-2"></div>
                                    <button type="button" class="btn btn-sm btn-outline-success mt-2" data-action="add-filter">
                                        <i class="bi bi-plus"></i> 新增过滤规则
                                    </button>
                                    <div class="form-text">过滤规则，支持正则表达式，每行一个规则</div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- 重命名配置 -->
                <div class="tab-pane fade" id="rename" role="tabpanel">
                    <div class="card mt-3">
                        <div class="card-body">
                            <form id="renameForm" class="row g-3">
                                <div class="col-12">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="rename_enable">
                                        <label class="form-check-label" for="rename_enable">
                                            启用AI智能重命名
                                        </label>
                                        <div class="form-text">使用AI分析文件名并重命名为标准格式</div>
                                    </div>
                                </div>

                                <div id="renameConfig" class="row g-3">
                                    <div class="col-md-12">
                                        <label for="rename_format" class="form-label">重命名格式</label>
                                        <input type="text" class="form-control" id="rename_format" placeholder="{name} S{season:02d}E{episode:02d}">
                                        <div class="form-text">控制输出文件名格式，支持番剧名、季、集等占位符</div>
                                    </div>
                                    <div class="col-md-4">
                                        <label for="rename_extractor_type" class="form-label">AI提供商</label>
                                        <select class="form-select" id="rename_extractor_type">
                                            <option value="">选择提供商</option>
                                            <option value="openai">OpenAI</option>
                                            <option value="google">Google Gemini</option>
                                            <option value="deepseek">DeepSeek</option>
                                        </select>
                                        <div class="form-text">选择AI服务提供商</div>
                                    </div>
                                    <div class="col-md-4">
                                        <label for="rename_model" class="form-label">模型</label>
                                        <input type="text" class="form-control" id="rename_model" placeholder="例如: gpt-4o-2024-11-20">
                                    </div>
                                    <div class="col-md-4">
                                        <label for="rename_api_key" class="form-label">API密钥</label>
                                        <input type="password" class="form-control" id="rename_api_key" placeholder="输入API密钥">
                                    </div>
                                    <div class="col-md-6" id="rename_base_url_group">
                                        <label for="rename_base_url" class="form-label">API Base URL</label>
                                        <input type="url" class="form-control" id="rename_base_url" placeholder="https://api.openai.com/v1">
                                    </div>
                                    <div class="col-md-6">
                                        <label for="rename_output_type" class="form-label">输出格式</label>
                                        <select class="form-select" id="rename_output_type">
                                            <option value="json_object">JSON Object</option>
                                            <option value="json_schema">JSON Schema</option>
                                        </select>
                                        <div class="form-text">选择提取器输出结构</div>
                                    </div>
                                    <div class="col-md-12">
                                        <div class="row g-3 align-items-center">
                                            <div class="col-md-4">
                                                <div class="form-check">
                                                    <input class="form-check-input" type="checkbox" id="rename_remap_enable">
                                                    <label class="form-check-label" for="rename_remap_enable">
                                                        启用重映射
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="col-md-8">
                                                <label for="rename_remap_path" class="form-label mb-1">重映射配置路径</label>
                                                <input type="text" class="form-control" id="rename_remap_path" placeholder="./remap.yaml">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- 通知配置 -->
                <div class="tab-pane fade" id="notification" role="tabpanel">
                    <div class="card mt-3">
                        <div class="card-body">
                            <form id="notificationForm">
                                <div class="row g-3 align-items-center">
                                    <div class="col-md-4">
                                        <div class="form-check form-switch">
                                            <input class="form-check-input" type="checkbox" id="notification_enable">
                                            <label class="form-check-label" for="notification_enable">
                                                启用通知
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <label for="notification_interval" class="form-label">通知间隔 (秒)</label>
                                        <input type="number" class="form-control" id="notification_interval" min="0" step="60">
                                    </div>
                                </div>

                                <div class="mt-4">
                                    <div class="d-flex align-items-center justify-content-between mb-2">
                                        <h6 class="mb-0">
                                            <i class="bi bi-robot me-2"></i>
                                            通知机器人
                                        </h6>
                                        <button type="button" class="btn btn-sm btn-outline-success" id="addNotificationBot">
                                            <i class="bi bi-plus"></i> 添加机器人
                                        </button>
                                    </div>
                                    <div id="notificationBots" class="d-flex flex-column gap-2"></div>
                                    <div class="form-text">支持 Telegram / PushPlus，至少配置一个机器人后再启用通知</div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- WebDAV配置 -->
                <div class="tab-pane fade" id="webdav" role="tabpanel">
                    <div class="card mt-3">
                        <div class="card-body">
                            <form id="webdavForm" class="row g-3">
                                <div class="col-12">
                                    <p class="text-muted small">将使用 Alist 配置中的 base_url 自动拼接 /dav 作为 WebDAV 地址。</p>
                                </div>
                                <div class="col-md-4">
                                    <label for="webdav_username" class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="webdav_username" placeholder="admin">
                                </div>
                                <div class="col-md-4">
                                    <label for="webdav_password" class="form-label">密码</label>
                                    <input type="password" class="form-control" id="webdav_password">
                                </div>
                                <div class="col-md-4">
                                    <label for="webdav_timeout" class="form-label">连接超时 (秒)</label>
                                    <input type="number" class="form-control" id="webdav_timeout" min="5" max="300" value="30">
                                </div>

                                <div class="col-12">
                                    <h6 class="border-bottom pb-2 mb-3 mt-2">
                                        <i class="bi bi-tools me-2"></i>
                                        自动修复
                                    </h6>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="webdav_fixer_enable">
                                        <label class="form-check-label" for="webdav_fixer_enable">启用自动修复</label>
                                        <div class="form-text">下载完成后自动执行嵌套目录修复</div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="webdav_execute_mode">
                                        <label class="form-check-label" for="webdav_execute_mode">实际执行</label>
                                        <div class="form-text">关闭则仅预览（dry-run）</div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="webdav_recursive_scan">
                                        <label class="form-check-label" for="webdav_recursive_scan">递归扫描</label>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <label for="webdav_conflict_strategy" class="form-label">冲突策略</label>
                                    <select class="form-select" id="webdav_conflict_strategy">
                                        <option value="skip">跳过</option>
                                        <option value="rename">重命名</option>
                                        <option value="overwrite">覆盖</option>
                                    </select>
                                </div>

                                <div class="col-12">
                                    <h6 class="border-bottom pb-2 mb-3 mt-2">
                                        <i class="bi bi-lightning-charge me-2"></i>
                                        手动修复默认值
                                    </h6>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="webdav_manual_enable">
                                        <label class="form-check-label" for="webdav_manual_enable">允许手动修复</label>
                                    </div>
                                </div>
                                <div class="col-md-8">
                                    <label for="webdav_manual_path" class="form-label">默认路径</label>
                                    <input type="text" class="form-control" id="webdav_manual_path" placeholder="/115/TV/...">
                                    <div class="form-text">在 WebDAV 修复页面的默认目标目录</div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="webdav_manual_execute">
                                        <label class="form-check-label" for="webdav_manual_execute">默认执行模式</label>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="webdav_manual_recursive">
                                        <label class="form-check-label" for="webdav_manual_recursive">默认递归</label>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <label for="webdav_manual_strategy" class="form-label">默认冲突策略</label>
                                    <select class="form-select" id="webdav_manual_strategy">
                                        <option value="skip">跳过</option>
                                        <option value="rename">重命名</option>
                                        <option value="overwrite">覆盖</option>
                                    </select>
                                </div>

                                <div class="col-12">
                                    <div class="alert alert-info d-flex align-items-center" role="alert">
                                        <i class="bi bi-lightning-charge-fill me-2"></i>
                                        <div>
                                            手动触发一次性修复已迁移至 <a href="/webdav/manual" class="alert-link">WebDAV修复</a> 页面。
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 操作按钮 -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="d-flex flex-column flex-md-row justify-content-between gap-2">
                <div class="d-flex flex-column flex-sm-row gap-2">
                    <button type="button" class="btn btn-outline-secondary" id="btnTestConfig">
                        <i class="bi bi-check-circle me-2"></i>
                        测试配置
                    </button>
                    <button type="button" class="btn btn-outline-warning" id="btnResetConfig">
                        <i class="bi bi-arrow-counterclockwise me-2"></i>
                        重置为默认
                    </button>
                </div>
                <div>
                    <button type="button" class="btn btn-success w-100 w-md-auto" id="btnSaveConfig">
                        <i class="bi bi-save me-2"></i>
                        保存配置
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 导入配置模态框 -->
<div class="modal fade" id="importModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">导入配置</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label for="importFile" class="form-label">选择配置文件</label>
                    <input type="file" class="form-control" id="importFile" accept=".yaml,.yml,.json">
                    <div class="form-text">支持YAML或JSON格式的配置文件</div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="btnDoImport">导入</button>
            </div>
        </div>
    </div>
</div>

<!-- 配置验证模态框 -->
<div class="modal fade" id="validationModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">配置验证结果</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div id="validationResults"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script type="module" src="{{ url_for('static', path='js/config.js') }}"></script>
{% endblock %}
